/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.freshdirect.src.dao;

import com.freshdirect.src.model.Supplier;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;

/**
 *
 * @author Ilias
 */
public class SupplierDAOImpl implements SupplierDAO{
    
    private DataSource dataSource;
    
    @Override
    public void setDataSource(String filename) {
        Properties props = new Properties();
        InputStream fis = null;
        MysqlDataSource mysqlDS = null;
        try {
            fis = getClass().getResourceAsStream("/"+filename+".properties");
            props.load(fis);
            mysqlDS = new MysqlDataSource();
            mysqlDS.setURL(props.getProperty("DB_URL"));
            mysqlDS.setUser(props.getProperty("DB_USERNAME"));
            mysqlDS.setPassword(props.getProperty("DB_PASSWORD"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.dataSource = mysqlDS;
    }
    
    @Override
    public List<Supplier> getAll() {
        String query = "call getallsuppliers();";
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Supplier> sups = new ArrayList<Supplier>();
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();
            while(rs.next()){
                Supplier sup = new Supplier();
                sup.setId(rs.getInt(1));
                sup.setName(rs.getString(2));
                sup.setEmail(rs.getString(3));
                sups.add(sup);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return sups;
    }

    @Override
    public String getAllSupplierstoString() {
        String query = "call getallsuppliers();";
        String sups = "";
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            con = dataSource.getConnection();
            ps = con.prepareStatement(query);
            rs = ps.executeQuery();
            while(rs.next()){
                sups += rs.getInt(1) + ",";
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally{
            try {
                rs.close();
                ps.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return sups;
    }
    
}
